<template>
  <div>
      <t-table
        :data="data"
        :columns="ORDER_COLUMNS"
        row-key="orderno"
        vertical-align="top"
        :hover="true"
        :pagination="pagination"
        :loading="dataLoading"
        :selected-row-keys="selectedRowKeys"
        @page-change="rehandlePageChange"
      >
        <template #order_status="{ row }">
          <t-tag v-if="row.order_status === 1" variant="light"> 待付款 </t-tag>
          <t-tag v-if="row.order_status === 2" variant="light"> 待发货 </t-tag>
          <t-tag v-if="row.order_status === 3" variant="light"> 待收货 </t-tag>
          <t-tag v-if="row.order_status === 4" variant="light"> 己关闭 </t-tag>
          <t-tag v-if="row.order_status === 5" theme="primary" variant="light"> 己完成 </t-tag>
          <t-tag v-if="row.order_status === 7" variant="light"> 售后中 </t-tag>
          <t-tag v-if="row.order_status === 8" variant="light"> 待分享 </t-tag>
        </template>
        
        <template #pay_type="{ row }">
          <t-tag v-if="row.pay_type === 1" variant="light"> 微信支付 </t-tag>
          <t-tag v-if="row.pay_type === 2" variant="light"> 支付宝支付 </t-tag>
          <t-tag v-if="row.pay_type === 3" variant="light"> 积分支付 </t-tag>
          <t-tag v-if="row.pay_type === 4" variant="light"> 抖音支付 </t-tag>
        </template>
        <template #op="slotProps">
          <a v-if="slotProps.row.order_status!=5" class="t-button-link" @click="queryOrderPayInfo(slotProps)">查支付结果</a>
        </template>
      </t-table>

  </div>
</template>

<script setup type="ts">
  import { ref,reactive, computed } from 'vue';
  import { useRouter } from 'vue-router';
  import { MessagePlugin } from 'tdesign-vue-next';
  import { getOrderList ,payQuery} from '@/api/order';
  import { prefix } from '@/config/global';
  import {ORDER_COLUMNS} from './constants'


const data = ref([]);
const pagination = reactive({
  current: 1,
  pageSize: 20,
  total: 0,
});
const dataLoading = ref(false);
const confirmVisible = ref(false);
const selectedRowKeys = ref([]);
const drawerVisible = ref(false);
const fetchData =  (paginationInfo) => {
  dataLoading.value = true;
  getOrderList(paginationInfo).then(ret=>{
     const {items,totalItems,currentPage}=ret
     data.value = items;
     pagination.total = totalItems;
     pagination.current = currentPage;

  }).catch(e=>{
    console.log('e  ',JSON.stringify(e))

  }).finally(()=>{
    dataLoading.value = false;
  })

};
fetchData({
    page: pagination.current,
    pageSize: pagination.pageSize,
  });
// 刷新数据
const refresh = async () => {
  pagination.current = 1;
  await fetchData(pagination);
};
const rehandlePageChange = (pageInfo) => {
  console.log(`rehandlePageChange${JSON.stringify(pageInfo)}`);
  pagination.current = pageInfo.current;
  pagination.pageSize = pageInfo.pageSize;
  fetchData({page:pageInfo.current,pageSize:pageInfo.pageSize});
};

const queryOrderPayInfo=(d)=>{
  var info=d.row
  payQuery(info)
  .then(ret=>{
    let i= data.value.findIndex(o=>o.orderno===info.orderno)
    if(i!==-1)
    {
      data.value[i]=ret
    }
    MessagePlugin.success("己发起请求")
  }).catch(e=>{
    MessagePlugin.error(e.msg)
  })
}

</script>
